<template>
  <el-drawer
    :title="drawerTitle[drawerType]"
    size="60%"
    :visible.sync="drawerVisible"
    direction="rtl"
    :close-on-press-escape="false"
    :wrapperClosable="false"
    :destroy-on-close="true"
    :before-close="clearDialog"
    custom-class="tag-drawer"
    v-drawerResize
  >
    <div class="drawer-wrap">
      <div class="drawer-content tag-scrollbar">
        <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="标签名称" prop="tagNm">
                <el-input
                  placeholder="请输入标签名称"
                  v-model="editForm.tagNm"
                  :disabled="drawerType === 'edit'"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="枚举值" prop="cdVal">
                <el-input
                  placeholder="请输入枚举值"
                  v-model="editForm.cdVal"
                  :disabled="drawerType === 'edit'"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="枚举值描述" prop="cdDesc">
                <el-input
                  placeholder="请输入枚举值描述"
                  v-model="editForm.cdDesc"
                  type="textarea"
                  :maxlength="500"
                  :rows="4"
                  resize="none"
                  :show-word-limit="true"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <div class="drawer-btns">
      <el-button @click="clearDialog">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </div>
  </el-drawer>
</template>

<script>
export default {
  data() {
    return {
      drawerVisible: false,
      drawerType: 'add',
      drawerTitle: {
        add: '新增',
        edit: '编辑'
      },
      editForm: {
        tagId: '',
        tagNm: '',
        cdVal: '',
        cdDesc: ''
      },
      editFormRules: {
        tagNm: [{ required: true, message: '标签名称不能为空', trigger: 'blur' }],
        cdVal: [
          { required: true, message: '枚举值不能为空', trigger: 'blur' },
          { max: 100, message: '枚举值不能超过100个字符', trigger: 'blur' }
        ],
        cdDesc: [
          { required: true, message: '枚举值描述不能为空', trigger: 'blur' },
          { max: 500, message: '枚举值描述不能超过500个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    showDrawer(type, data) {
      this.drawerType = type
      if (type === 'edit') {
        this.editForm.tagId = data.tagId
        this.editForm.tagNm = data.tagNm
        this.editForm.cdVal = data.cdVal
        this.editForm.cdDesc = data.cdDesc
      }
      this.drawerVisible = true
    },
    submit() {
      this.$refs['editForm'].validate(valid => {
        if (valid) {
          let params = {
            tagNm: this.editForm.tagNm,
            cdVal: this.editForm.cdVal,
            cdDesc: this.editForm.cdDesc
          }
          if (this.drawerType === 'edit') params.tagId = this.editForm.tagId
          let url = this.drawerType === 'edit' ? 'ast/astTagEnumVal/update' : 'ast/astTagEnumVal/save'
          this.$ajax({
            url: this.$ajax.setUrl(url),
            method: 'post',
            data: this.$ajax.setData(params)
          }).then(({ data }) => {
            if (data && data.code === '0') {
              this.clearDialog()
              this.$emit('refresh')
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 2000
              })
            } else {
              this.$message.error(data.data || data.msg)
            }
          })
        }
      })
    },
    clearDialog() {
      this.editForm.tagId = ''
      this.editForm.tagNm = ''
      this.editForm.cdVal = ''
      this.editForm.cdDesc = ''
      this.$nextTick(() => {
        this.$refs['editForm'].clearValidate()
      })
      this.drawerVisible = false
    }
  }
}
</script>

<style lang="scss"></style>
